<template>
    <div>
      <div id="box">
        <h1>登录界面</h1>
        <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
          <el-form-item label="用户名" prop="username">
            <el-input v-model="ruleForm.username"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="pass">
            <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
            <el-button @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </template>
    
  <script>
  export default {
    data() {
      var checkAge = (rule, value, callback) => {
        if (!value) {
          return callback(new Error("用户名不能为空"));
        } else if (value != "magpie") {
          callback(new Error("你输入的用户名不对"));
        } else {
          callback();
        }
      };
      var validatePass = (rule, value, callback) => {
        if (value == 123456) {
          callback();
        } else if (!value) {
          callback(new Error("密码不能为空"));
        } else {
          callback(new Error("你输入的密码不对"));
        }
      };
      return {
        ruleForm: {
          pass: "",
          username: "",
        },
        rules: {
          pass: [{ validator: validatePass, trigger: "blur" }],
          username: [{ validator: checkAge, trigger: "blur" }],
        },
      };
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            alert("提交成功");
            localStorage.setItem("用户名", this.ruleForm.username);
            localStorage.setItem("密码", this.ruleForm.pass);
            this.$router.push("/app1");
          } else {
            alert("提交失败");
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },
    },
  };
  </script>
    
  <style lang="less" scoped>
  #box {
    width: 400px;
    height: 300px;
    margin: auto;
    margin-top: 100px;
    outline: 1px solid black;
    box-shadow: 0px 0px 10px 5px gray;
    position: relative;
  }
  
  h1 {
    position: absolute;
    left: 140px;
    top: 30px;
  }
  
  .el-form {
    position: absolute;
    width: 80%;
    position: absolute;
    left: 20px;
    top: 100px;
  }
  </style>